package com.ruoyi.web.controller.mall;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Coupons;
import com.ruoyi.system.service.ICouponsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 * 
 * @author ruoyi
 * @date 2024-03-22
 */
@Controller
@RequestMapping("/mall/coupons")
public class CouponsController extends BaseController
{
    private String prefix = "mall/coupons";

    @Autowired
    private ICouponsService couponsService;

    @RequiresPermissions("mall:coupons:view")
    @GetMapping()
    public String coupons()
    {
        return prefix + "/coupons";
    }

    /**
     * 查询优惠券列表
     */
    @RequiresPermissions("mall:coupons:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Coupons coupons)
    {
        startPage();
        List<Coupons> list = couponsService.selectCouponsList(coupons);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @RequiresPermissions("mall:coupons:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Coupons coupons)
    {
        List<Coupons> list = couponsService.selectCouponsList(coupons);
        ExcelUtil<Coupons> util = new ExcelUtil<Coupons>(Coupons.class);
        return util.exportExcel(list, "优惠券数据");
    }

    /**
     * 新增优惠券
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存优惠券
     */
    @RequiresPermissions("mall:coupons:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Coupons coupons)
    {
        return toAjax(couponsService.insertCoupons(coupons));
    }

    /**
     * 修改优惠券
     */
    @RequiresPermissions("mall:coupons:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Coupons coupons = couponsService.selectCouponsById(id);
        mmap.put("coupons", coupons);
        return prefix + "/edit";
    }

    /**
     * 修改保存优惠券
     */
    @RequiresPermissions("mall:coupons:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Coupons coupons)
    {
        return toAjax(couponsService.updateCoupons(coupons));
    }

    /**
     * 删除优惠券
     */
    @RequiresPermissions("mall:coupons:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(couponsService.deleteCouponsByIds(ids));
    }
}
